package com.xc.sys.service;

import java.io.IOException;
import java.util.List;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SelectField;
import org.jooq.SelectForUpdateStep;
import org.jooq.SelectSeekStepN;
import org.jooq.SortField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xc.common.object.PageInfo;
import com.xc.db.Tables;
import com.xc.db.tables.SysNavi;
import com.xc.db.tables.records.SysNaviRecord;
import com.xc.sys.entity.SysNaviEntity;

@Service
public class SysNaviService {
	@Autowired
	private DSLContext dsl;
	private SysNavi NAVI = SysNavi.SYS_NAVI.as("NAVI");
	public List<SysNaviEntity> select(Condition cond, SortField<?>... orderby) {
		Result<Record> result = null;
		if (cond == null) {
			result = dsl.select().from(this.NAVI).orderBy(orderby).fetch();
		} else {
			SelectSeekStepN<Record> su = dsl.select().from(this.NAVI).where(cond).orderBy(orderby);
			result = su.fetch();
		}
		List<SysNaviEntity> list = result.into(SysNaviEntity.class);
		return list;
	}
	public PageInfo<SysNaviEntity> select(Condition cond, PageInfo<SysNaviEntity> pageInfo,SortField<?>... orderby) {
		Result<Record> result = null;
		if (cond == null) {
			result = this.dsl.select(new SelectField[0]).from(this.NAVI).orderBy(orderby).limit(pageInfo.getLimit().intValue()).offset(pageInfo.getBeginNum()).fetch();
		} else {
			SelectForUpdateStep<Record> su = dsl.select(new SelectField[0]).from(this.NAVI).where(cond).orderBy(orderby).limit(pageInfo.getLimit().intValue()).offset(pageInfo.getBeginNum());
			result = su.fetch();
		}
		pageInfo.setResult(result);
		pageInfo.setList(result.into(SysNaviEntity.class));
		return pageInfo;
	}
	public Long count(Condition cond) {
		Long count = null;
		count = (Long) dsl.selectCount().from(this.NAVI).where(new Condition[] { cond }).fetchOneInto(Long.class);
		return count;
	}
	public Record getById(Integer id) {
		Record record = dsl.select(new SelectField[0]).from(NAVI).where(new Condition[] { NAVI.ID.eq(id) }).fetchOne();
		return record;
	}
	public Record getById(String id) {
		Record record = getById(Integer.valueOf(id));
		return record;
	}

	public SysNaviEntity getEntityById (Integer id) {
		SysNaviEntity entity = null;
		Record record = dsl.select().from(NAVI).where(new Condition[] { NAVI.ID.eq(id) }).fetchOne();
		if(record!=null) {
			entity = record.into(SysNaviEntity.class);
		}
		return entity;
	}
	public SysNaviEntity getEntityById(String id) {
		SysNaviEntity entity = getEntityById( Integer.valueOf(id) );
		return entity;
	}

	public void update(SysNaviEntity entity) throws IOException {
		if (entity.getParentId()!=null&&entity.getParentId().equals("")) {
			entity.setParentId(null);
			entity.setTier(Integer.valueOf(1));
		} else {
			if(entity.getParentId()==null) {
				entity.setTier(1);
			}else {
				Record pRecord = getById(entity.getParentId());
				entity.setTier(pRecord.get(NAVI.TIER) + 1);
			}
		}
		SysNaviRecord record = new SysNaviRecord();
		record.from(entity);
		dsl.executeUpdate(record);
	}

	public void update(Record record) {
		if (record.get(NAVI.PARENT_ID)==null) {
			record.set(NAVI.PARENT_ID, null);
		} else {
			Record pRecord = getById(record.get(NAVI.PARENT_ID));
			record.set(this.NAVI.TIER, Integer.valueOf(((Integer) pRecord.get(this.NAVI.TIER)).intValue() + 1));
		}
		SysNaviRecord sysNaviRecord = new SysNaviRecord();
		sysNaviRecord.from(record);
		this.dsl.executeUpdate(sysNaviRecord);
	}

	public Integer insert(SysNaviEntity entity) {
		if (entity.getParentId().equals("")) {
			entity.setParentId(null);
			entity.setTier(Integer.valueOf(1));
		} else {
			Record pRecord = getById(entity.getParentId());
			entity.setTier(((Integer) pRecord.get(this.NAVI.TIER)).intValue() + 1);
		}
		SysNaviRecord record = new SysNaviRecord();
		record.from(entity);
		int id = dsl.insertInto(Tables.SYS_NAVI).set(record).returning(NAVI.ID).fetchOne().getId();
		entity.setId(id + "");
		return id;
	}

	public void deleteById(Integer id) {
		dsl.delete(NAVI).where(new Condition[] { NAVI.ID.eq( id ) }).execute();
	}
	public void deleteById(String id) {
		dsl.delete(NAVI).where(new Condition[] { NAVI.ID.eq( Integer.valueOf(id) ) }).execute();
	}
}
